package com.example.taobaounion.ui.adapter

import android.content.Context
import android.graphics.Paint
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.taobaounion.R

import com.example.taobaounion.model.bean.CategoryPagerData
import com.example.taobaounion.utils.UrlUtil
import java.util.ArrayList

/**
 *@author : Administrator
 *@descreption : 每个分类页面的内容的recyclerView的适配器
 */
class CategoryContentAdapter : RecyclerView.Adapter<CategoryContentAdapter.Holder>() {
    private var mOnGoodsItemClick: OnGoodsItemClick? = null
    private var mDataList = ArrayList<CategoryPagerData.DataBean>()
    private lateinit var mContext: Context
    private var resultPrice = 0.0f
    var i = 0
    var ii = 0

    fun setOnGoodsItemClick(onGoodsItemClick: OnGoodsItemClick) {
        this.mOnGoodsItemClick = onGoodsItemClick
    }


    fun setData(beanList: List<CategoryPagerData.DataBean>) {
        mDataList.clear()
        mDataList.addAll(beanList)
        notifyDataSetChanged()
    }

    class Holder(itemView: View) : RecyclerView.ViewHolder(itemView) {

        //整体
        var item: LinearLayout = itemView.findViewById(R.id.item_view)

        //图像
        val goodsImageIV: ImageView = itemView.findViewById(R.id.good_image_iv)

        //货品名称
        var titleTv: TextView = itemView.findViewById(R.id.goods_title)

        //优惠券额度
        var goodsOffPriceTv: TextView = itemView.findViewById(R.id.goods_off_price)

        //券后价
        var goodsAfterOffPriceTv: TextView = itemView.findViewById(R.id.goods_after_off_price)

        //原价
        var originalPriceTv: TextView = itemView.findViewById(R.id.orignal_price_tv)

        //已卖数量
        var salesAmountTv: TextView = itemView.findViewById(R.id.sales_amount)

    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): Holder {
        mContext = parent.context
        val rootView =
            LayoutInflater.from(parent.context).inflate(R.layout.item_category_good_layout, parent, false)
        return Holder(rootView)

    }

    override fun getItemCount(): Int {
        return mDataList.size

    }

    override fun onBindViewHolder(holder: Holder, position: Int) {
        Glide.with(mContext).load(UrlUtil.getHttpsUrl(mDataList[position].pict_url)).into(holder.goodsImageIV)
        //resultPrice:券后价格
        resultPrice =
            (mDataList[position].zk_final_price).toFloat() - mDataList[position].coupon_amount
        holder.titleTv.text = mDataList[position].title
        //结果格式:省%1$d元 如:省20元
        holder.goodsOffPriceTv.text =
            String.format(mContext.resources.getString(R.string.text_off_price), mDataList[position].coupon_amount)
        //结果只保留2位小数
        holder.goodsAfterOffPriceTv.text = String.format("%.2f", resultPrice)
        //原价
        holder.originalPriceTv.paintFlags = Paint.STRIKE_THRU_TEXT_FLAG
        holder.originalPriceTv.text =
            String.format(mContext.resources.getString(R.string.text_final_price), mDataList[position].zk_final_price)
        //以卖数量
        holder.salesAmountTv.text =
            String.format(mContext.resources.getString(R.string.text_sales_amount), mDataList[position].volume.toInt())
        holder.item.setOnClickListener {

            if (mOnGoodsItemClick!=null){
                if (mDataList[position].coupon_share_url!=null){
                    mOnGoodsItemClick!!.onItemClickCallBack(mDataList[position].coupon_share_url,mDataList[position].title,UrlUtil.getHttpsUrl(mDataList[position].pict_url))
                }else{
                    mOnGoodsItemClick!!.onItemClickCallBack(mDataList[position].click_url,mDataList[position].title,UrlUtil.getHttpsUrl(mDataList[position].pict_url))
                }
            }
        }
    }

    interface OnGoodsItemClick {
        fun onItemClickCallBack(url: String, title: String, httpsUrl: String)
    }
}